/* Hawk - An OSGI Based Test Framework
 *
 * Copyright (C) 2010 http://code.google.com/p/hawk-framework/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package org.hawk.gui;

import java.util.Locale;
import java.util.ResourceBundle;

import org.hawk.gui.windows.ExecutionWindow;
import org.hawk.gui.windows.MainWindow;
import org.hawk.gui.windows.TestSessionWindow;

import com.vaadin.Application;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.MenuBar.Command;
import com.vaadin.ui.MenuBar.MenuItem;

/**
 * This class initializes the web interface of the Hawk Test Framework
 * 
 * @author Rodrigo Prestes Machado
 * @version November 15, 2009
 */
@SuppressWarnings("serial")
public class HawkWeb extends Application {

	/** Main window instance (background) */
	private MainWindow mainWindow;
	
	/** Localized System messages */
	private static ResourceBundle messages;

	/**
	 * This method is responsible to initialize the Hawk user system interface
	 */
	public void init() {
		
		// Setting application messages
		//TODO Remove the "en_US" from the code
		Locale currentLocale = new Locale("en","US"); 
		messages = ResourceBundle.getBundle("MessagesBundle", currentLocale);
		
		// Creating main window
		mainWindow = MainWindow.getInstance(messages.getString("HawkWeb.title"));
		setMainWindow(mainWindow);

		// Creating Menu
		MenuBar menuBar = new MenuBar();
		menuBar.setSizeFull();
		
		// Adding menu itens
		menuBar.addItem(messages.getString("HawkWeb.menu.exec"), new Commands());
		menuBar.addItem(messages.getString("HawkWeb.menu.session"), new Commands());
		
		// Adding menu bar
		mainWindow.addComponent(menuBar);
	}
	
	/**
	 * Returns the system messages
	 * 
	 * @return ResourceBundle messages
	 */
	public static ResourceBundle getMessages() {
		return messages;
	}
	
	/**
	 * This class implements actions for all menus items
	 */
	public class Commands implements Command {
		@Override
		public void menuSelected(MenuItem selectedItem) {
			
			if (selectedItem.getText().equals(messages.getString("HawkWeb.menu.exec")))
				mainWindow.addWindow(new ExecutionWindow(messages));
			else
				mainWindow.addWindow(new TestSessionWindow(messages));
		}
	}
	
}